package by.blog.web.servlets;

import by.blog.web.commands.AddCommand;
import by.blog.web.commands.Command;
import by.blog.web.commands.EditCommand;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 *web.xml: /action.jsp
 * This class execute all commands from main menu(main.jsp)
 * -->single.jsp edit
 * -->single.jsp add
 */
public class MenuController extends HttpServlet {


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html; charset=UTF-8");
        request.setCharacterEncoding("UTF-8");

        String action = request.getParameter("action");
        request.setAttribute("action", action);

        Command command;

        if ("edit".equals(action)) {
            command = new EditCommand();
            command.execute(request, response);
        }

        if ("add".equals(action)) {
            command = new AddCommand();
            command.execute(request, response);
        }


        //RequestDispatcher dispatcher = request.getRequestDispatcher("/main.jsp");
        //dispatcher.forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }


}
